package com.example.demo.loadclass;

import java.lang.reflect.Method;

public class ClassHotSwap {
    public static void main(String[] args) throws Exception {
        while(true) {
            try {
                MyClassLoader myLoader = new MyClassLoader("D:/MyLoader/MyLoader.class");
                Class cls = myLoader.loadClass("com.example.demo.loadclass.MyLoader");
                Object demo = cls.newInstance();
                Method m = demo.getClass().getMethod("printSth", new Class[] {});
                m.invoke(demo, new Object[] {});
                Thread.sleep(2000);
            } catch(Exception e) {
                e.printStackTrace();
                System.out.println("找不到类.");
                try {
                    Thread.sleep(2000);
                } catch(InterruptedException ex) {

                }
            }
        }
    }

}
